<?php
    /**
     * 网站基础信息设置类
     * @author Administrator
     *
     */
    class admin_model_siteprofile extends model
    {
        public $id = '';

        public function __construct()
        {
            parent::__construct();
            $this->table_name = config::get()->table->site_profile;
        }

        /**
         * 获取所有设置信息
         */
        public function get_list()
        {
            $sql = "select * from `{$this->table_name}`";

            return $this->db->query($sql)->fetch_all()->query_result;
        }

        public function get_key_value()
        {
            $res = $this->get_list();
            $new_res = array();

            foreach($res AS $v)
            {
                $new_res[$v['key']] = $v['value'];
            }

            return $new_res;
        }

        /**
         * 读取一个设置信息
         * @param $id
         */
        public function load($id)
        {
            $sql = "select * from `{$this->table_name}` where `id` = {$id}";

            return $this->db->query($sql)->fetch_one()->query_result;
        }

        /**
         * 保存配置信息，用于新增和编辑.
         */
        public function save($name, $key, $style)
        {
            if ($this->id == '')
            {
                //新增
                $sql = "insert into `{$this->table_name}`(`name`, `key`, `value`, `style`) values('{$name}', '{$key}', '', '{$style}')";
            }
            else
            {
                //更新
                $sql = "update `{$this->table_name}` set `name` = '{$name}', `key` = '{$key}', `style` = '{$style}' where `id` = {$this->id}";
            }

            $this->db->query($sql);

            if ($this->db->query_result)
            {
                return TRUE;
            }
            else
            {
                return FALSE;
            }
        }

        /**
         * 设置配置ID
         * @param unknown_type $id
         */
        public function set_id($id)
        {
            $this->id = $id;
        }

        /**
         * 删除ID为$id的配置
         * @param unknown_type $id
         */
        public function delete($id)
        {
            $sql = "delete from `{$this->table_name}` where `id` = '{$id}'";

            $this->db->query($sql);

            if ($this->db->query_result)
            {
                return TRUE;
            }
            else
            {
                return FALSE;
            }
        }

        /**
         * 更新网站配置
         * @param $data
         */
        public function update($data)
        {
            $sql = '';

            foreach($data AS $k => $v)
            {
                $sql = "update `{$this->table_name}` set `value` = '{$v}' where `key` = '{$k}';";

                $this->db->query($sql);
            }

            return TRUE;
        }
    }